
<template>
    <div class="agritainment">
        <Search @searchData="getAgritainmentData" @resetData="getAgritainmentData"></Search>
        <AddAndDel @delsByIds="delsByIds"></AddAndDel>
        <el-card>
            <el-table ref="multipleTable" class="custom-table" height="450px"
                :default-sort="{ prop: 'date', order: 'descending' }"
                :header-cell-style="{ background: '#f5f7fa', color: '#606266' }" :data="tableData" tooltip-effect="dark"
                style="width: 100%" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55">
                </el-table-column>
                <el-table-column prop="id" label="序号" width="100" sortable>
                    <!-- <template slot-scope="scope">{{ scope.row.id }}</template> -->
                </el-table-column>
                <el-table-column prop="img" label="农家乐主图" width="120">
                    <template slot-scope="scope">
                        <!-- <img :src="scope.row.img" alt=""> -->
                        <el-image class="img" :src="scope.row.img" :preview-src-list="srcList"></el-image>
                    </template>
                </el-table-column>
                <el-table-column prop="title" label="农家乐标题" width="280" show-overflow-tooltip>
                </el-table-column>
                <el-table-column prop="content" label="农家乐内容">
                    <template slot-scope="scope">
                        <el-button type="text" size="mini" @click="handleDetail(scope.row.content)">点击查看</el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="createdDt" label="发布时间">
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleEdit(scope.row)" plain>编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)" plain>删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <Pagination :paginationData="paginationData" @getPaginationData="getAgritainmentData"></Pagination>
        </el-card>
        <AgritainmentDialog @addAgritainmentInfo="addAgritainmentInfo" @getAgritainmentData="getAgritainmentData">
        </AgritainmentDialog>
        <DetailDialog :content="content"></DetailDialog>
    </div>
</template>
<script>
import Search from '../../components/search/index.vue'
import AddAndDel from '../../components/add_and_del/index.vue'
import Pagination from '../../components/pagination/index.vue'
import AgritainmentDialog from './dialog/index.vue'
import DetailDialog from '../../components/detail_dialog/index.vue'
export default {
    name: 'Agritainment',
    components: {
        Search,
        AddAndDel,
        Pagination,
        AgritainmentDialog,
        DetailDialog
    },
    mixins: [],
    props: {

    },
    data() {
        return {
            tableData: [],
            multipleSelection: [],
            paginationData: {},
            content: '',
            srcList: []
        }
    },
    computed: {

    },
    watch: {

    },
    mounted() {
        this.getAgritainmentData()
    },
    methods: {
        // 获取农家乐信息
        getAgritainmentData(obj = { currentPage: 1, pageSize: 10, keyword: '' }) {
            this.request.post('/farmInfo/selectFarmInfoPage', obj).then(res => {
                if (res.code == 20000) {
                    res.data.list.forEach(item => {
                        if (item.createdDt) {
                            item.createdDt = item.createdDt.slice(0, 10)
                        }
                    })
                    this.tableData = res.data.list
                    this.paginationData = res.data
                    this.srcList = res.data.list.map(item => item.img)
                }
            })
        },
        // 农家乐信息 新增 编辑
        addAgritainmentInfo(obj) {
            this.request.post('/farmInfo/farmInfoAdit', obj).then(res => {
                if (res.code == 20000) {
                    // 重新获取农家乐信息列表
                    this.getAgritainmentData()

                }
            })
        },
        //批量删除
        handleSelectionChange(selectedData) {
            // 被选中的数据的id
            this.multipleSelection = selectedData.map(item => item.id);
            this.$bus.$emit('multipleSelection', this.multipleSelection)
        },
        // 批量删除
        delsByIds() {
            this.request.post('/farmInfo/farmInfoDelByIds', { ids: this.multipleSelection }).then(res => {
                if (res.code == 20000) {
                    this.getAgritainmentData()
                    this.$message({
                        type: 'success',
                        message: '删除成功~'
                    })
                }
            })
        },
        // 编辑农家乐信息
        handleEdit(row) {
            this.$bus.$emit('add', true)
            this.$bus.$emit('getForm', row)
        },
        // 删除 农家乐信息
        handleDelete(row) {
            this.$confirm('是否确认删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.request.post('/farmInfo/farmInfoDel', { id: row.id }).then(res => {
                    if (res.code == 20000) {
                        this.getAgritainmentData()
                        this.$message({
                            type: 'success',
                            message: '删除成功~'
                        })

                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        },
        handleDetail(content) {
            this.content = content
            this.$bus.$emit('showDetailDialog', true)
        }
    }
};
</script>
<style lang='less' scoped>
.agritainment {
    .el-table {
        font-size: 12px;
    }

    .img {
        width: 80px;
        height: 40px;
        cursor: pointer;
    }

    .el-pagination.is-background .el-pager li:not(.disabled).active {
        background-color: #11A983 !important;
        color: #FFF;
    }

}
</style>
